﻿using System.Diagnostics.Contracts;
using System.Web.Mvc;
using Cik.Gapi4Net;
using Cik.Gapi4Net.Domain.Input;
using MVCSample.UI.Extensions;

namespace MVCSample.UI.Controllers
{
    public class HomeController : MultiViewController
    {
        #region private variables & ctors

        private IGApi4Net _gapi4net;
        private const string COMMON_SEARCH_TITLE = "Manchester United";

        public HomeController()

            : this(new GApi4Net())
        {
        }

        public HomeController(IGApi4Net gapi4net)
        {
            _gapi4net = gapi4net;
        }

        #endregion private variables & ctors

        #region public actions

        public ActionResult Web()
        {
            Contract.Assert(_gapi4net != null, "Gapi4Net is null");

            var model = _gapi4net.WebSearch(() => GetJsonFactory<Web>(COMMON_SEARCH_TITLE)).MappingWebResult();
            SetTitle(model, WrappingSearchText(COMMON_SEARCH_TITLE));

            return View(model);
        }

        [HttpPost]
        public ActionResult Web(string searchText, int startWith)
        {
            Contract.Assert(_gapi4net != null, "Gapi4Net is null");

            var model = _gapi4net.WebSearch(() => PostJsonFactory<Web>(searchText, startWith)).MappingWebResult();
            SetTitle(model, WrappingSearchText(searchText));

            return GetJsonResult("_Web", model);
        }

        public ActionResult Local()
        {
            Contract.Assert(_gapi4net != null, "Gapi4Net is null");

            var model = _gapi4net.LocalSearch(() => GetJsonFactory<Local>(COMMON_SEARCH_TITLE)).MappingLocalResult();
            SetTitle(model, WrappingSearchText(COMMON_SEARCH_TITLE));

            return View(model);
        }

        [HttpPost]
        public ActionResult Local(string searchText, int startWith)
        {
            Contract.Assert(_gapi4net != null, "Gapi4Net is null");

            var model = _gapi4net.LocalSearch(() => PostJsonFactory<Local>(searchText, startWith)).MappingLocalResult();
            SetTitle(model, WrappingSearchText(searchText));

            return GetJsonResult("_Local", model);
        }

        public ActionResult Video()
        {
            Contract.Assert(_gapi4net != null, "Gapi4Net is null");

            var model = _gapi4net.VideoSearch(() => GetJsonFactory<Video>(COMMON_SEARCH_TITLE)).MappingVideoResult();
            SetTitle(model, WrappingSearchText(COMMON_SEARCH_TITLE));

            return View(model);
        }

        [HttpPost]
        public ActionResult Video(string searchText, int startWith)
        {
            Contract.Assert(_gapi4net != null, "Gapi4Net is null");

            var model = _gapi4net.VideoSearch(() => PostJsonFactory<Video>(searchText, startWith)).MappingVideoResult();
            SetTitle(model, WrappingSearchText(searchText));

            return GetJsonResult("_Video", model);
        }

        public ActionResult Blog()
        {
            Contract.Assert(_gapi4net != null, "Gapi4Net is null");

            var model = _gapi4net.BlogSearch(() => GetJsonFactory<Blog>(COMMON_SEARCH_TITLE)).MappingBlogResult();
            SetTitle(model, WrappingSearchText(COMMON_SEARCH_TITLE));

            return View(model);
        }

        [HttpPost]
        public ActionResult Blog(string searchText, int startWith)
        {
            Contract.Assert(_gapi4net != null, "Gapi4Net is null");

            var model = _gapi4net.BlogSearch(() => PostJsonFactory<Blog>(searchText, startWith)).MappingBlogResult();
            SetTitle(model, WrappingSearchText(searchText));

            return GetJsonResult("_Blog", model);
        }

        public ActionResult News()
        {
            Contract.Assert(_gapi4net != null, "Gapi4Net is null");
            //var newsResult = _gapi4net.NewsSearch("1.0", "Google");
            var model = _gapi4net.NewsSearch(() => GetJsonFactory<News>(COMMON_SEARCH_TITLE)).MappingNewsResult();
            SetTitle(model, WrappingSearchText(COMMON_SEARCH_TITLE));

            return View(model);
        }

        [HttpPost]
        public ActionResult News(string searchText, int startWith)
        {
            Contract.Assert(_gapi4net != null, "Gapi4Net is null");

            var model = _gapi4net.NewsSearch(() => PostJsonFactory<News>(searchText, startWith)).MappingNewsResult();
            SetTitle(model, WrappingSearchText(searchText));

            return GetJsonResult("_News", model);
        }

        public ActionResult Book()
        {
            Contract.Assert(_gapi4net != null, "Gapi4Net is null");

            var model = _gapi4net.BookSearch(() => GetJsonFactory<Book>(COMMON_SEARCH_TITLE)).MappingBookResult();
            SetTitle(model, WrappingSearchText(COMMON_SEARCH_TITLE));

            return View(model);
        }

        [HttpPost]
        public ActionResult Book(string searchText, int startWith)
        {
            Contract.Assert(_gapi4net != null, "Gapi4Net is null");

            var model = _gapi4net.BookSearch(() => PostJsonFactory<Book>(searchText, startWith)).MappingBookResult();
            SetTitle(model, WrappingSearchText(searchText));

            return GetJsonResult("_Book", model);
        }

        public ActionResult Image()
        {
            Contract.Assert(_gapi4net != null, "Gapi4Net is null");

            var model = _gapi4net.ImageSearch(() => GetJsonFactory<Image>(COMMON_SEARCH_TITLE)).MappingImageResult();
            SetTitle(model, WrappingSearchText(COMMON_SEARCH_TITLE));

            return View(model);
        }

        [HttpPost]
        public ActionResult Image(string searchText, int startWith)
        {
            Contract.Assert(_gapi4net != null, "Gapi4Net is null");

            var model = _gapi4net.ImageSearch(() => PostJsonFactory<Image>(searchText, startWith)).MappingImageResult();
            SetTitle(model, WrappingSearchText(searchText));

            return GetJsonResult("_Image", model);
        }

        public ActionResult Patent()
        {
            Contract.Assert(_gapi4net != null, "Gapi4Net is null");

            var model = _gapi4net.PatentSearch(() => GetJsonFactory<Patent>(COMMON_SEARCH_TITLE)).MappingPatentResult();
            SetTitle(model, WrappingSearchText(COMMON_SEARCH_TITLE));

            return View(model);
        }

        [HttpPost]
        public ActionResult Patent(string searchText, int startWith)
        {
            Contract.Assert(_gapi4net != null, "Gapi4Net is null");

            var model = _gapi4net.PatentSearch(() => PostJsonFactory<Patent>(searchText, startWith)).MappingPatentResult();
            SetTitle(model, WrappingSearchText(searchText));

            return GetJsonResult("_Patent", model);
        }

        #endregion public actions

        #region private methods

        private string WrappingSearchText(string searchString)
        {
            return "Result search on: '" + searchString + "'";
        }

        #endregion private methods
    }
}